package com.zzvcom.mail;

import java.security.Security;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.log4j.Logger;

/**
 * 使用Gmail发送邮件
 * 
 * @author fay lai
 */
public class GmailSender implements Runnable
{

    private SendParam param;

    static Logger logger = Logger.getLogger(GmailSender.class);

    public static void main(String[] args) 
    {
        
        SendParam param=new SendParam();
        param.setUsername("faylai");
        param.setPassword("717+818=LOVE");
        param.setSubject("yilong mail test");
        param.setText("helle kitty <br/> <font color=red>赖君玉</font>");
        param.setTo("251435816@qq.com,faylai@tom.com");
        GmailSender gs=new GmailSender();
        gs.send(param);
        
    }

    public void run()
    {
        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
        // Get a Properties object
        Properties props = System.getProperties();
        props.setProperty("mail.smtp.host", "smtp.gmail.com");
        props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.port", "465");
        props.setProperty("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.auth", "true");
        final String username = param.getUsername().split("@")[0];
        final String password = param.getPassword();
        Session session = Session.getDefaultInstance(props, new Authenticator()
        {
            protected PasswordAuthentication getPasswordAuthentication()
            {
                return new PasswordAuthentication(username, password);
            }
        });

        // -- Create a new message --
        Message msg = new MimeMessage(session);
        // -- Set the FROM and TO fields --
        try
        {
            msg.setFrom(new InternetAddress(username + "@gmail.com"));
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(param.getTo(), false));
            msg.setSubject(param.getSubject());
            //msg.setText(param.getText());
            msg.setSentDate(new Date());  
            BodyPart bp=new MimeBodyPart();
            bp.setContent(param.getText(),"text/html;charset=utf-8");
            Multipart mm=new MimeMultipart();
            mm.addBodyPart(bp);
            msg.setContent(mm);
            Transport.send(msg);
        }
        catch (Exception e)
        {            
            logger.error(e.getMessage());
            e.printStackTrace();
        }
    }

    public void send(SendParam param)
    {
        this.param = param;
        new Thread(this).start();

    }

}